/**
 * Created by shuhongji on 2017/7/17.
 */
//导航栏

var totalNumber = 0;
// var totalPage = 1;
// var currentPage = 1;
var lastPage = 1;

$(function () {
    init();
    $(".course-list .list1 a").click(function () {
        var url;
        if (this.href.indexOf("quanbu") >= 0 ){
            $(".list1 ul li").removeClass("on");
            $(this).parent().addClass("on");
            $(".list2 ul").html("" +
                "<li class='list-item on'>\n" +
                "   <a href='quanbu'>全部</a></li>\n" +
                "<li class='list-item'>\n" +
                "   <a href='java'>Java</a></li>\n" +
                "<li class='list-item'>\n" +
                "   <a href='c%2B%2B'>C++</a></li>\n" +
                "<li class='list-item'>\n" +
                "   <a href='html'>Html</a></li>\n" +
                "");
            url = '/api/search';
            console.log(url);
        }else if(this.href.indexOf("qianduan") >= 0 ){
            $(".list1 ul li").removeClass("on");
            $(this).parent().addClass("on");
            $(".list2 ul").html("" +
                "<li class='list-item on'>\n" +
                "   <a href='quanbu'>全部</a></li>\n" +
                "<li class='list-item'>\n" +
                "   <a href='html'>Html</a></li>\n" +
                "");
            url = '/api/search?direction=qianduan';
            console.log(url);
        }else if(this.href.indexOf("houduan") >= 0 ){
            $(".list1 ul li").removeClass("on");
            $(this).parent().addClass("on");
            $(".list2 ul").html("" +
                "<li class='list-item on'>\n" +
                "   <a href='quanbu'>全部</a></li>\n" +
                "<li class='list-item'>\n" +
                "   <a href='java'>Java</a></li>\n" +
                "<li class='list-item'>\n" +
                "   <a href='c%2B%2B'>C++</a></li>\n" +
                "");
            url = '/api/search?direction=houduan';
            console.log(url);
        }else{
            return false;
        };
        init();
        getData(url)
        return false;
    });

    //初始化点击事件
    function init () {
        $(".course-list .list2 a").click(function () {
            var url;
            console.log(this.href);
            if (this.href.indexOf("quanbu") >= 0 ) {
                $(".list2 ul li").removeClass("on");
                $(this).parent().addClass("on");
                var href = $(".list1 ul .on a").attr("href");
                if (href != "quanbu"){
                    url = '/api/search?direction=' + href;
                }else {
                    url = '/api/search';
                }
                console.log(url);
            } else if (this.href.indexOf("java") >= 0 ) {
                $(".list2 ul li").removeClass("on");
                $(this).parent().addClass("on");
                url = '/api/search?type=java';
                console.log(url);
            } else if (this.href.indexOf("c%2B%2B") >= 0 ) {
                $(".list2 ul li").removeClass("on");
                $(this).parent().addClass("on");
                url = '/api/search?type=c%2B%2B';
                console.log(url);
            } else if (this.href.indexOf("html") >= 0 ) {
                $(".list2 ul li").removeClass("on");
                $(this).parent().addClass("on");
                url = '/api/search?type=html';
                console.log(url);
            } else {
                return false;
            }
            getData(url);
            return false;
        });
    }

})

function getUrl(index) {
    var h1 = $(".course-list .list1 ul .on a").attr("href");
    var h2 = $(".course-list .list2 ul .on a").attr("href");
    if (h2 != "quanbu"){
        return "?type=" + h2 + "&page=" + index;
    }else{
        if (h1 != "quanbu"){
            return "?direction=" + h1 + "&page=" + index;
        }else{
            return "?page=" + index;
        }
    }
}

function getTotalNumber() {
    // totalNumber = 101;
    return totalNumber;
}

function getTotalPage() {
    var totalPage = Math.ceil(getTotalNumber()/20)
    return totalPage;
}

function getCurrentPage() {
    return parseInt($(".onpage").text());
}

//绑定点击事件
function initClick() {
    var a = $(".base-index a");
    var l = a.length;
    var url;
    $(a[0]).click(function () {
        url = "/api/search" + getUrl(1);
        getData(url);
        initIndex();
        return false;
    });
    $(a[1]).click(function () {
        if(getCurrentPage() > 1){
            var a = "a[value="+ (getCurrentPage() - 1) +"]";
            // url = "/api/search" + getUrl($(a).attr("value"));
            // getData(url);
            $(a).click();
        }
    });
    $(a[l-1]).click(function () {
        url = "/api/search" + getUrl(getTotalPage());
        getData(url);
        setIndex(getTotalPage()-4,getTotalPage(),getTotalPage());
        return false;
    });
    $(a[l-2]).click(function () {
        if(getCurrentPage() < getTotalPage()){
            var a = "a[value="+ (getCurrentPage() + 1) +"]";
            // url = "/api/search" + getUrl($(a).attr("value"));
            // getData(url);
            $(a).click();
        }
    });
    for(var i = 2;i <= l-3 ; i++) {
        $(a[i]).click(function () {
            url = "/api/search" + getUrl($(this).attr("value"));
            getData(url);
            lastPage = parseInt($(".onpage").text());
            $(a).removeClass("onpage");
            $(this).addClass("onpage");
            // console.log("current:"+getCurrentPage());
            // console.log("last:"+lastPage)
        });
    }

    // 分页脚本
    if(getTotalPage() > 5){

        $($(".base-index a")[5]).click(function () {
            if (lastPage < getCurrentPage()){
                if(getCurrentPage() + 2 > getTotalPage()){
                    if (getCurrentPage() + 1 > getTotalPage()){

                    }else{
                        setIndex(getCurrentPage()-3,getCurrentPage()+1,getCurrentPage());
                    }
                }else{
                    setIndex(getCurrentPage()-2,getCurrentPage()+2,getCurrentPage());
                }

            }
        });

        $($(".base-index a")[6]).click(function () {
            if (lastPage < getCurrentPage()){
                if (getCurrentPage() + 2 > getTotalPage()){
                    if(getCurrentPage() + 1 > getTotalPage()){

                    }else{
                        setIndex(getCurrentPage()-3,getCurrentPage()+1,getCurrentPage());
                    }
                }else{
                    setIndex(getCurrentPage()-2,getCurrentPage()+2,getCurrentPage());
                }

            }
        });

        $($(".base-index a")[3]).click(function () {
            if(lastPage > getCurrentPage()){
                if (getCurrentPage() - 2 > 0){
                    setIndex(getCurrentPage()-2,getCurrentPage()+2,getCurrentPage());
                }

            }

        });

        $($(".base-index a")[2]).click(function () {
            if(lastPage > getCurrentPage()){
                if (getCurrentPage() - 2 > 0){
                    setIndex(getCurrentPage()-2,getCurrentPage()+2,getCurrentPage());
                }else if(getCurrentPage() - 1 > 0){
                    setIndex(getCurrentPage()-1,getCurrentPage()+3,getCurrentPage());
                }

            }
        });
    }
}

//动态设定分页编号
function setIndex(start,end,current) {
    var html = "" +
        "<a class='page-index'>首页</a>\n" +
        "<a class='page-prev'>上一页</a>\n" +
        "";
    for(var i = start; i<=end ; i++){
        if (i == current){
            html += "<a class='page onpage' value='"+ i +"'>"+ i +"</a>\n";
        }else{
            html += "<a class='page' value='"+ i +"'>"+ i +"</a>\n";
        }

    }
    html += "<a class='page-next'>下一页</a>\n" +
        "<a class='page-last'>尾页</a>"
    // console.log(html);
    $(".base-index").html(html);
    initClick();
}

//初始化分页编号
function initIndex() {
    var n;
    if ((n = getTotalPage()) <= 5){
        setIndex(1,n,1)
    }else{
        setIndex(1,5,1);
    }
}

//使用AJAX获取数据,返回json数据
function getData(url) {
    // alert("ajax地址:" + "http://192.168.15.107" + url)
    $.ajax({
        async:false,
        type:"get",
        url:"http://192.168.15.107" + url,
        dataType:"json",
        success:function (data) {
            totalNumber = data.total;
            if(url.indexOf("page") < 0){
                initIndex();
            }
            getMovive(data);
        },
        error:function (XMLHttpRequest, textStatus, errorThrown) {
            alert("error");
            return null;
        }
    });
}

function setHeight(size) {
    var n = Math.ceil(size/4);
    if (n >= 5){
        $(".middle").css("height","1000px");
    }else{
        var h = (n * 210) + "px";
        $(".middle").css("height",h);
    }
}

//绑定数据
function getMovive(data) {
    var movies = $(".base-middle ul");
    var html = "";
    for(var i = 0 ; i < data.movie.length ; i++){
        html +="<li>\n" +
            "<a href=detail.html?id=" + data.movie[i].id + " class='movie-box'>\n" +
            "<div class='box-detail'>\n" +
            "<img src=" + data.movie[i].url + " style='max-width: 100%'>\n" +
            "<p>" + data.movie[i].title + "</p>\n" +
            "</div>\n" +
            "</a>\n" +
            "</li>\n";
    }
    movies.html(html);
    setHeight(data.movie.length);
}

$(function () {
    // var data = '{"total":3,"movie":[' +
    //     '{"href":"h1","url":"images/546_G_1480752213812.jpg","title":"t1"},' +
    //     '{"href":"h2","url":"images/546_G_1480752213812.jpg","title":"t2"},' +
    //     '{"href":"h3","url":"images/546_G_1480752213812.jpg","title":"t3"}' +
    //     ']}';
    //
    // data = JSON.parse(data);
    getData("/api/search");

})